package keeto.com.vn.classified.task;

import java.io.IOException;

import keeto.com.vn.classified.core.ClassifiedCore;
import keeto.com.vn.classified.core.ClassifiedListener;
import keeto.com.vn.classified.network.IProtocol;
import keeto.com.vn.classified.request.GetSearchGlobalProductRequest;
import keeto.com.vn.classified.response.SearchResponse;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
public class taskGetSearchGlobalProduct extends AbstractTask{
	private String AreaIds, CategoryType, Keywords;
	private int IsDisabled,pageLimit, pageNumber;
	private String categoryid;
	


	public taskGetSearchGlobalProduct(ClassifiedListener listener,
			ClassifiedCore core, String areaIds, String categoryType,
			String keywords, int isDisabled, int pageLimit, int pageNumber,
			String categoryid) {
		super(listener, core);
		AreaIds = areaIds;
		CategoryType = categoryType;
		Keywords = keywords;
		IsDisabled = isDisabled;
		this.pageLimit = pageLimit;
		this.pageNumber = pageNumber;
		this.categoryid = categoryid;
	}

	@Override
	public void run() throws ClientProtocolException, JSONException, IOException {
		GetSearchGlobalProductRequest request = new GetSearchGlobalProductRequest();
		request.setAreaIds(AreaIds);
		request.setIsDisable(IsDisabled);
		request.setCategoryType(CategoryType);
		request.setPageLimit(pageLimit);
		request.setPageNumber(pageNumber);
		request.setKeywords(Keywords);
		request.setCategoryId(categoryid);
		SearchResponse response = json.getGlobalSearch(request);
		for (ClassifiedListener listener : listeners) {
			listener.finishGetGlobalSearch(response.getReturnCode(),response.getItems(), response.isHasMore(), response.getPageCount());
		}
	}

	@Override
	public void timeoutCallback() {
		for (ClassifiedListener l : core.copyAndReconstructListeners(listener)) {
			l.finishGetGlobalSearch(IProtocol.STATUS_CODE_CONNECTION_TIMEOUT_502, null, false,0);
		}
		
	}

}
